/**
 * 
 */
package com.paypal.melearning.ui.component;

import java.util.Arrays;

import com.paypal.melearning.domain.ContentType;
import com.vaadin.data.Item;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;


/**
 * @author prakrishnan
 *
 */
public class PinFieldFactory extends DefaultFieldFactory {

	private static final String COMMON_FIELD_WIDTH = "25em";

	/* (non-Javadoc)
	 * @see com.vaadin.ui.FormFieldFactory#createField(com.vaadin.data.Item, java.lang.Object, com.vaadin.ui.Component)
	 */
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f;
        if ("type".equals(propertyId)) {
            // Create a password field so the password is not shown
            f = createContentTypeField(propertyId);
        } else {
            // Use the super class to create a suitable field base on the
            // property type.
            f = super.createField(item, propertyId, uiContext);
        }

        if ("pinName".equals(propertyId)) {
            TextField tf = (TextField) f;
            tf.setRequired(true);
            tf.setCaption("Please enter a Pin Name");
            tf.setInputPrompt("Pin Name");
            tf.setRequiredError("Please enter a Pin Name");
            tf.setWidth(COMMON_FIELD_WIDTH);
            tf.addValidator(new StringLengthValidator(
                    "Pin Name must be 3-25 characters", 3, 25, false));
        } else if ("contentURL".equals(propertyId)) {
            TextField tf = (TextField) f;
            tf.setRequired(true);
            tf.setCaption("Please enter a Content URL");
            tf.setInputPrompt("Pin URL");
            tf.setRequiredError("Please enter a Content URL");
            tf.setWidth(COMMON_FIELD_WIDTH);
            tf.addValidator(new StringLengthValidator(
                    "Content URL must be 3-50 characters", 3, 50, false));
        } else if ("description".equals(propertyId)) {
        	TextField tf = (TextField) f;
            tf.setWidth("20em");
        }else if ("type".equals(propertyId)) {
        	ComboBox tf = (ComboBox) f;
            tf.setInvalidAllowed(false);
            
        }

        return f;

	}

	private Field createContentTypeField(Object propertyId) {
		ComboBox contentType = new ComboBox();
		contentType.setInputPrompt("Please select a Type");
		for(ContentType type : Arrays.asList(ContentType.values())){
			contentType.addItem(type.name());	
		}

		return contentType;
	}

}
